﻿using PMIS.WebApi.Model;

namespace PMIS.WebApi.Services
{
    public class MenuAuthorizeService : BaseService<MenuAuthorize>, IMenuAuthorizeService
    {
        public async Task<int> DeleteMenuAuthorize(string roleId)
        {
            return await SugarClient.Deleteable<MenuAuthorize>().Where(x => x.RoleId == roleId).ExecuteCommandAsync();
        }

        public async Task<int> AddMenuAuthorize(List<MenuAuthorize> items)
        {
            return await SugarClient.Insertable(items).ExecuteCommandAsync();
        }

        public async Task<List<MenuAuthorize>> GetMenuAuthorizeList(string roleId)
        {
            return await SugarClient.Queryable<MenuAuthorize>().Where(x => x.RoleId == roleId).ToListAsync();
        }

        public async Task<List<string?>>? GetMenuIdList(List<string> roleIdList)
        {
            var result = await SugarClient.Queryable<MenuAuthorize>()
                                  .In(it => it.RoleId, roleIdList)
                                  .GroupBy(it => it.MenuId)
                                  .Select(it => it.MenuId)
                                  .ToListAsync();

            return result;
        }
    }
}


